<?php
require('functions/page_load.php');
require_once("../classes/class.phpmailer.php");

$users_nav = true;

// delete

if(isset($_GET['delete_user'])){

	$sql = "DELETE FROM users WHERE id = '".mysql_real_escape_string($_GET['delete_user'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "DELETE FROM tradesman WHERE user_id = '".mysql_real_escape_string($_GET['delete_user'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	
	$sql = "UPDATE jobs SET status = 'Closed', deleted = 1, job_active = 0 WHERE user_id = '".mysql_real_escape_string($_GET['delete_user'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	$results = '<div class="success_result">User deleted successfully.</div>';

}


// ban

if(isset($_GET['ban'])){

	$sql = "UPDATE users SET banned = 1 WHERE id = '".mysql_real_escape_string($_GET['ban'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	$results = '<div class="success_result">User banned successfully.</div>';

}

// unban

if(isset($_GET['unban'])){

	$sql = "UPDATE users SET banned = 0 WHERE id = '".mysql_real_escape_string($_GET['unban'])."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());

	$results = '<div class="success_result">User unbanned successfully.</div>';

}

require('includes/header.php');
?>

<div class="side_container">
<h2 class="container_header">Filters</h2>

<ul>
<li><a href="users.php">Active users</a></li>
<li><a href="users.php?show=banned">Banned users</a></li>
</ul>

</div>

<div class="main_container_wrapper">

	<div class="main_container">
	<h2 class="container_header">Users</h2>
	
	<a href="add_user.php" class="create_button">Create new user</a>
	
	<?php echo $results; ?>
	
	<?php
	
	$where_clause = ($_GET['show'] == 'banned') ? "banned = 1" : "banned != 1" ;
	
	$sql = "SELECT users.id, firstname, surname, email, last_login, company_name, tradesman.id AS tradesman_id FROM users 
			LEFT JOIN tradesman ON users.id = tradesman.user_id 
			WHERE ".$where_clause." ORDER BY id DESC";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rows = mysql_num_rows($query);
	
	if($rows > 0){
	
	echo '<table width="100%" cellpadding="0" cellspacing="0" class="cms_table paginate_table">';
	
	if($_GET['show'] == 'banned'){
	echo '<thead><tr><th align="left">Name</th><th align="left">Email</th><th align="left">Tradesman</th><th width="60">Unban</th></tr></thead>';
	}
	
	else{
	echo '<thead><tr><th align="left">Name</th><th align="left">Email</th><th align="left">Tradesman</th><th width="60">Ban</th><th>Delete</th></tr></thead>';
	}
	
	echo '<tbody>';
	
		while($rs=mysql_fetch_assoc($query)){
		
			if($_GET['show'] == 'banned'){
			echo '<tr><td align="left"><a href="edit_user.php?id='.$rs['id'].'">'.$rs['firstname'].' '.$rs['surname'].'</a></td><td align="left"><a href="mailto:'.$rs['email'].'">'.$rs['email'].'</a></td><td align="left"><a href="edit_tradesman.php?id='.$rs['tradesman_id'].'">'.$rs['company_name'].'</a></td><td><a onclick="confirm_unban(\'?show=banned&amp;unban='.$rs['id'].'\',\'this user\');" title="Unban"><img src="images/yes.png" alt="Ban" /></a></td></tr>';
			}
			
			else{
			echo '<tr><td align="left"><a href="edit_user.php?id='.$rs['id'].'">'.$rs['firstname'].' '.$rs['surname'].'</a></td><td align="left"><a href="mailto:'.$rs['email'].'">'.$rs['email'].'</a></td><td align="left"><a href="edit_tradesman.php?id='.$rs['tradesman_id'].'">'.$rs['company_name'].'</a></td><td><a onclick="confirm_ban(\'?ban='.$rs['id'].'\',\'this user\');" title="Ban"><img src="images/na.png" alt="Ban" /></a></td><td><a onclick="confirm_delete(\'?delete_user='.$rs['id'].'\',\'this user\');" title="Delete"><img src="images/delete.png" alt="Delete" /></a></td></tr>';						
			}

		}
		
	echo '</tbody></table>';
	
	}
	
	else{
	
	echo ($_GET['show'] == 'banned') ? '<p style="float: left; width: 100%; text-align: center;">There are currently no banned users.</p>' : '<p style="float: left; width: 100%; text-align: center;">There are currently no users.</p>' ;
	
	}
	
	
	?>

	</div>

</div>
	
<?php
	require('includes/footer.php');
?>